﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using h = functionService.shorts;

namespace ETALON
{
    public partial class FormCreateFileParam : Form
    {
        functionService.prm q;
        DataTable dtp; 
        string currentFileNameXML;  int selectIndexInComboBox2;
        public FormCreateFileParam()
        {
            InitializeComponent(); readDir(); dtp = new DataTable();  
            currentFileNameXML = ""; 
            
            q = new functionService.prm(loc.plp.PLP); 
            comboBox1.Text = loc.plp.PLP;
            loadToGRID();
            selectIndexInComboBox2 = 0;
        }

        private void FormCreateFileParam_Load(object sender, EventArgs e)
        {
            //createSimleTableParam("PLP\\PLP_Connection");// m(123);
        }
        private void wfe()
        {
 
        }
        private void addNewRow(DataTable dto, object ocod, object oname, object oprim)
        {
            DataRow r;
            r = dto.NewRow();
            r[0] = ocod; r[1] = oname; r[2] = oprim;
            dto.Rows.Add(r);
            dto.AcceptChanges();

        }
        private void loadToCIX(out DataTable ttt,out int[] cixo)
        {
            ttt = lookDir_And_ReadFile_To_DataTable("PLP").Copy();
            cixo = new int[ttt.Rows.Count]; int i = 0;
            foreach (DataRow r in ttt.Rows)
            {
                cixo[i] = Convert.ToInt32(r[0]); i++;
            }
        }
        private void deleteCodFromTable(ref DataTable ttt, int codDelete)
        {
            int i = ttt.Rows.Count; object o;
            for (int j = 0; j < i; j++)
            {
                o=ttt.Rows[j][0];
                if (Convert.ToInt32(o) == codDelete) { ttt.Rows[j].Delete(); }
            }
            ttt.AcceptChanges();
            ttt.WriteXml("PLP\\PLP.xml", XmlWriteMode.WriteSchema);
            //h.m(h.aa(ttt.Rows.Count, "ok"));
            dataGridView1.DataSource = ttt;
        }
        private void enumRead123(Enum enm, string fileNameXMLwithoutExt)
        {
            //int ix = 0;
            //Type tt = typeof(enm); System.Array ar = Enum.GetValues(tt);
            //int[] cip = new int[ar.Length]; int j = 0;
            //foreach (object o in ar)
            //{
            //    cip[j] = Convert.ToInt32(o); j++;
            //}
            //DataTable ttt; int[] cix;
            //loadToCIX(out ttt, out cix);
            ////ttt = lookDir_And_ReadFile_To_DataTable("PLP").Copy();
            ////int[] cix = new int[ttt.Rows.Count]; int i = 0;
            ////foreach (DataRow r in ttt.Rows)
            ////{
            ////    cix[i] = Convert.ToInt32(r[0]); i++;
            ////}
            //bool IsHere;// = false;
            //foreach (int p in cip)
            //{
            //    //h.m(p);
            //    IsHere = false;
            //    foreach (int x in cix)
            //    {
            //        //h.m(h.aa(p, "=", x));
            //        if (p == x) { IsHere = true; }
            //    }
            //    if (IsHere == false) addNewRow(ttt, p, 0, Enum.GetName(tt, p));
            //}

            //ttt.WriteXml(string.Concat("PLP\\", fileNameXMLwithoutExt, ".xml"), XmlWriteMode.WriteSchema);
            //dataGridView1.DataSource = ttt;
            ////h.m(h.aa(ttt.Rows.Count, "ok"));
            //this.Close();
        }
        private void enumRead123()
        {
            int ix = 0;
            Type tt = typeof(param); System.Array ar = Enum.GetValues(tt);
            
            int[] cip = new int[ar.Length]; int j = 0;
            foreach (object o in ar)
            {
                cip[j] = Convert.ToInt32(o); j++;
            }
            DataTable ttt; int[] cix;
            loadToCIX(out ttt,out cix);
            //ttt = lookDir_And_ReadFile_To_DataTable("PLP").Copy();
            //int[] cix = new int[ttt.Rows.Count]; int i = 0;
            //foreach (DataRow r in ttt.Rows)
            //{
            //    cix[i] = Convert.ToInt32(r[0]); i++;
            //}
            bool IsHere;// = false;
            foreach (int p in cip)
            {
                //h.m(p);
                IsHere = false;
                foreach (int x in cix)
                {
                    //h.m(h.aa(p, "=", x));
                    if (p == x) { IsHere = true; }
                }
                if (IsHere == false) addNewRow(ttt, p, 0, Enum.GetName(tt, p));
            }

            ttt.WriteXml("PLP\\PLP.xml", XmlWriteMode.WriteSchema);
            dataGridView1.DataSource = ttt;
            //h.m(h.aa(ttt.Rows.Count, "ok"));
            this.Close();
        }
        private DataTable lookDir_And_ReadFile_To_DataTable(string fileName)
        {
            DataTable t = new DataTable();
            string s = "";
            if (lookDir(fileName))
            {
                s = string.Concat("PLP\\", fileName, ".xml");
                t.ReadXml(s);
                return t;
            }
            else return null;
        }
        private bool  lookDir(string fileName)
        {
            bool IsHere = false;
            DirectoryInfo di = new DirectoryInfo("PLP");
            FileInfo[] fai = di.GetFiles();
            foreach (FileInfo fi in fai)
            {
                if (fileName == fi.Name.Replace(".xml", "")) { IsHere = true; break; } 
            }
            return IsHere;
        }
        
        private void lookDirAnOpenFile(string fileName)
        {
            string s = "";
            if (lookDir(fileName)) 
            {
                if (dtp.Rows.Count > 0) { dtp.Clear(); }
                s = string.Concat("PLP\\", fileName, ".xml"); functionService.shorts.m(s);
                dtp.ReadXml(s);currentFileNameXML = fileName;
                q.sh(dtp);
            }
        }
        private void lookDir_And_ReadFile_To_DataTableDTP(string fileName)
        {
            DataTable dtl;
            string s = "";
            if (lookDir(fileName))
            {
                if (dtp.Rows.Count > 0) { dtp.Clear(); }
                s = string.Concat("PLP\\", fileName, ".xml"); functionService.shorts.m(s);
                dtp.ReadXml(s); currentFileNameXML = fileName; 
            }
        }
        private void lookDirAnOpenFileServers()
        {
            currentFileNameXML = "PLP_Connection";
            DataTable tc = new DataTable();
            if (lookDir("PLP_Connection"))
            {
                tc.ReadXml("PLP\\PLP_Connection.xml"); functionService.shorts.m(tc.Rows.Count);
            }
        }
        private void readDir()
        {
            DirectoryInfo di = new DirectoryInfo("PLP");
            FileInfo[] fai =  di.GetFiles();
            comboBox1.Items.Clear();
            foreach (FileInfo fi in fai)
            {
                comboBox1.Items.Add(fi.Name.Replace(".xml",""));
            }
        }
        private void createSimleTableParam(string fileNameWithoutXML)
        {
            DataTable t = new DataTable("tp");
            t.Columns.Add("cod", typeof(int));
            t.Columns.Add("name", typeof(string));
            t.Columns.Add("prim", typeof(string));
            t.AcceptChanges();
            DataRow r;
            r = t.NewRow();
            r[0] = 1; r[1] = "ol fio"; r[2] = "ol prim";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 2; r[1] = "ol fio2"; r[2] = "ol prim2";
            t.Rows.Add(r);
            r = t.NewRow();
            r[0] = 3; r[1] = "ol fio3"; r[2] = "ol prim3";
            t.Rows.Add(r);
            t.Columns[0].AutoIncrement = true;
            t.AcceptChanges();

            t.WriteXml(string.Concat(fileNameWithoutXML, ".xml"), XmlWriteMode.WriteSchema );
        }
        void m(object oo) { System.Windows.Forms.MessageBox.Show(oo.ToString()); }
        private string filePLP(string fileNameWithoutExt)
        {
            return string.Concat("PLP\\", fileNameWithoutExt, ".xml");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (lookDir(comboBox1.Text) == false) { createSimleTableParam(string.Concat("PLP\\", comboBox1.Text)); readDir(); m("Файл создан"); }
            else { m(string.Concat("Файл с именем ", comboBox1.Text, " уже существует")); }
        }
        private void save(string nameFileWithoutExt)
        {
            dtp.WriteXml(filePLP(nameFileWithoutExt), XmlWriteMode.WriteSchema);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            string stxt = comboBox1.Text; //fs.shorts.m(stxt);
            lookDirAnOpenFile(stxt);
            save(stxt);
            // dtp.WriteXml(filePLP(stxt),XmlWriteMode.WriteSchema);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataTable t = new DataTable(); t.ReadXml("pro_listParam.xml"); GRID.FormGRID f = new GRID.FormGRID(t); f.ShowDialog();
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            string stxt = comboBox1.Text;
            if (lookDir(stxt)) 
            {
                if (MessageBox.Show(string.Concat("Файл с именем ", stxt, " БУДЕТ удален"),
                    "Подтвердите Ваше намерение", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    File.Delete(filePLP(stxt)); readDir(); m(string.Concat("Файл с именем ", stxt, " УДАЛЕН"));
                }
                else { m("Отмена"); }
            }
        }

       

        private void readTable(DataTable dto)
        {
              
        }
        
        
        private void loadToGRID()
        {
            q.readDTP(); //q.sh(dtp);
            dataGridView1.DataSource = q.dtp;
            dataGridView1.Columns[0].Width = 30;
            dataGridView1.Columns[1].Width = 250;
            dataGridView1.Columns[2].Width = 165;
        }
        private void loadToGRID_fromDTP()
        {
            dataGridView1.DataSource = q.dtp;
            dataGridView1.Columns[0].Width = 30;
            dataGridView1.Columns[1].Width = 250;
            dataGridView1.Columns[2].Width = 165;
        }
        private void button6_Click(object sender, EventArgs e)
        {
            setParam();
            
        }
        private void setParam()
        { 
                int i = q.currentParamCOD  ;//Convert.ToInt32( comboBox2.Text);
                string s = textBox1.Text;
                q.put(i, s); 
            loadToGRID();selectedRows(selectIndexInComboBox2);
        }
        private void setPrim()
        {
            int i = q.currentParamCOD;//Convert.ToInt32( comboBox2.Text);
            string s = textBox2.Text;
            q.putPrim(i, s); loadToGRID(); //selectedRows(selectIndexInComboBox2);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            setPrim();
        }
        private void selectedRows(int indexSelect)
        {
            for (int j = 0; j < dataGridView1.Rows.Count; j++)
            {
                dataGridView1.Rows[j].Selected = false ;
                if (j == indexSelect) dataGridView1.Rows[j].Selected = true ;
            }
        }
        

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            readGridCells();
        }
        private void readGridCells()
        {
            object o1,o2,o3;
            o1 = dataGridView1.SelectedRows[0].Cells[0].Value;
            o2 = dataGridView1.SelectedRows[0].Cells[1].Value;
            o3 = dataGridView1.SelectedRows[0].Cells[2].Value;
            setControls(o1,o2,o3);

        }
        private void setControls(object o1,object o2, object o3)
        {
           q.currentParamCOD = Convert.ToInt32(o1);
           label2.Text = o1.ToString();
            textBox1.Text = o2.ToString();
            textBox2.Text = o3.ToString();
        }

        private void toolStripDropDownButton1_Click(object sender, EventArgs e)
        {
            lookDirAnOpenFileServers();
        }

   
        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            q.currentFileName = comboBox1.Text; //currentFileNameXML = s;
            //fs.shorts.m(s);
            q.readAnyXML_fromPLP_Dir_ToTableDTP(q.currentFileName); //q.sh(dtp);
            loadToGRID_fromDTP(); //loadItemsTo_ComboBox2();
            
            //fs.shorts.m(s);


           // dataGridView1.DataSource = ;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            enumRead123();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int ci = dataGridView1.SelectedRows[0].Index;
            DataTable t = lookDir_And_ReadFile_To_DataTable("PLP").Copy();
            deleteCodFromTable(ref t, Convert.ToInt32(label2.Text));
            dataGridView1.Rows[ci].Selected = true;
        }

    }
}
